package djrain.lilochat.client;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.NinePatch;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class OverlayMan extends Overlay
{

	private GeoPoint geoPoint;
	private Bitmap bitmap;
	private String text = "";
	private Paint paint;

	public OverlayMan( Bitmap bitmap)
	{
		create(geoPoint,bitmap,"");
	}
	
	public void create( GeoPoint geoPoint, Bitmap bitmap, String text)
	{
		this.geoPoint = geoPoint;
		this.text = text;
		this.bitmap = bitmap;
		
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setStrokeWidth(5);
		paint.setAlpha(120);
		paint.setTextSize(30);
		
		
	
		
	}

	public void setGeoPoint(GeoPoint geoPoint)
	{
		this.geoPoint = geoPoint;
	}
	
	public GeoPoint getGeoPoint()
	{
		return geoPoint;
	}
	
	public void setBitmap(Bitmap bitmap)
	{
		this.bitmap = bitmap;
	}
	
	public Bitmap getBitmap()
	{
		return bitmap;
	}
	
	public void setText(String text)
	{
		this.text = text;
	}
	
	public String getText()
	{
		return text;
	}
	

	@Override
	public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
	{
		Drawable d = mapView.getContext().getResources().getDrawable(R.drawable.bubble);
		
		if(geoPoint == null ) return false;
		Projection projection = mapView.getProjection();
		if (shadow == false)
		{
			Point point = new Point();
			projection.toPixels(geoPoint, point);
			canvas.drawBitmap(bitmap, point.x, point.y, paint);
			
			canvas.drawText("text", point.x, point.y-20, paint);
			Rect bounds = new Rect();
			paint.getTextBounds("text", 0, "text".length(), bounds);
			bounds.offsetTo(point.x, point.y-20-bounds.height());

			Bitmap bitmapb = Bitmap.createBitmap(bounds.width(),bounds.height()+30, Bitmap.Config.ARGB_8888);
			Canvas canvasb = new Canvas(bitmapb);
			d.setBounds(0, 0, bounds.width(),bounds.height()+30);
			d.draw(canvasb);

			
//			canvas.drawRect(bounds,  paint);
			canvas.drawBitmap(bitmapb,point.x, point.y-(bounds.height()+30), paint);
			
		}
		return super.draw(canvas, mapView, shadow, when);
	}
}
